package com.tomcov.server;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.tomcov.client.service.JourneyPassengerService;
import com.tomcov.shared.Journey;
import com.tomcov.shared.JourneyPassenger;

public class JourneyPassengerServiceImpl extends RemoteServiceServlet implements JourneyPassengerService {

	private static final long serialVersionUID = 1L;

	@Override
	public JourneyPassenger addJourney(JourneyPassenger jp) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(com.tomcov.shared.JourneyPassenger.class);
		query.declareImports("import java.util.Date");
		query.setFilter("Passenger == FromParam && date == dateParam && Heuredep == HeuredepParam");
		query.declareParameters("String FromParam, Date dateParam, Date HeuredepParam");
		try{
			@SuppressWarnings("unchecked")
			List<JourneyPassenger> results = (List<JourneyPassenger>) query.execute(jp.getPassenger(),
							jp.getDate(),jp.getHeuredep());
			if(results.size()==0){
				pm.makePersistent(jp);
				return jp;
			}else{
				return null;
			}
		}
		finally{
			query.closeAll();
			pm.close();
		}
	}

	@Override
	public JourneyPassenger delete(JourneyPassenger jp) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(com.tomcov.shared.JourneyPassenger.class);
		query.setFilter("id == FromParam && Passenger == DriverParam");
		query.declareParameters("String FromParam, String DriverParam");

		try{
			@SuppressWarnings("unchecked")
			List<JourneyPassenger> results = (List<JourneyPassenger>) query.execute(jp.getId(),jp.getPassenger());
			if(results.size()==0){
				return null;
			}
			else{
				JourneyPassenger res= results.get(0);
				pm.deletePersistent(res);
				return res;
			}
		}
		finally{
			query.closeAll();
			pm.close();
		}
	}

	public List<JourneyPassenger> getJourneyAtSameDate(Journey J){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(com.tomcov.shared.JourneyPassenger.class);
		query.setFilter("date == ArrParam");
		query.declareImports("import java.util.Date");
		query.declareParameters("Date ArrParam");
		try{
			@SuppressWarnings("unchecked")
			List<JourneyPassenger> results = (List<JourneyPassenger>) query.execute(J.getDate());
			if(results.size()==0){
				return null;
			}
			else{
				return (List<JourneyPassenger>) pm.detachCopyAll(results);
			}
		}
		finally{
			query.closeAll();
			pm.close();
		}
	}
	
	public List<JourneyPassenger> getJourneyByName2(String Username){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(com.tomcov.shared.JourneyPassenger.class);
		query.setFilter("Passenger == PassParam && DriverName != Stringz");
		query.declareParameters("String PassParam,String Stringz");
		try{
			@SuppressWarnings("unchecked")
			List<JourneyPassenger> results = (List<JourneyPassenger>) query.execute(Username,"null");
			if(results.size()==0){
				return null;
			}
			else{
				return (List<JourneyPassenger>) pm.detachCopyAll(results);
			}
		}
		finally{
			query.closeAll();
			pm.close();
		}
	}
	
	public List<JourneyPassenger> getJourneyByName(String Username){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(com.tomcov.shared.JourneyPassenger.class);
		query.setFilter("Passenger == PassParam");
		query.declareParameters("String PassParam");
		try{
			@SuppressWarnings("unchecked")
			List<JourneyPassenger> results = (List<JourneyPassenger>) query.execute(Username);
			if(results.size()==0){
				return null;
			}
			else{
				return (List<JourneyPassenger>) pm.detachCopyAll(results);
			}
		}
		finally{
			query.closeAll();
			pm.close();
		}
	}
	
	public JourneyPassenger delete(String id){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(com.tomcov.shared.JourneyPassenger.class);
		query.declareImports("import java.util.Arrays");
		query.setFilter("id == FromParam ");
		query.declareParameters("String FromParam");

		try{
			@SuppressWarnings("unchecked")
			List<JourneyPassenger> results = (List<JourneyPassenger>) query.execute(id);
			if(results.size()==0){
				return null;
			}
			else{
				JourneyPassenger res= results.get(0);
				pm.deletePersistent(res);
				return res;
			}
		}
		finally{
			query.closeAll();
			pm.close();
		}
	}

	@Override
	public JourneyPassenger adddriver(JourneyPassenger jp) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			JourneyPassenger e = pm.getObjectById(com.tomcov.shared.JourneyPassenger.class, jp.getId());
			e.setDriverName(jp.getDriverName());
			
			return jp;

		} finally {
			pm.close();
		}
	}

	@Override
	public JourneyPassenger getJourneyById(String journeypassidnumber) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			JourneyPassenger e = pm.getObjectById(com.tomcov.shared.JourneyPassenger.class, journeypassidnumber);
			JourneyPassenger jp =e;
			return jp;

		} finally {
			pm.close();
		}
	}
	
}
